$(function(){
	// 返回按钮
		$("#goback").click(function(){
			history.go(-1);
		});
	// 设置按钮
	$(".settings").on("click",function(){
		$(".settingsC").slideToggle();
	});
	var timer = "";
	var totalHeight = $("#reply")[0].scrollHeight+44+$("#themeSection").height();
	
		// 回到顶部事件
		$("#backTop").click(function(event){
			$("html,body").animate({scrollTop:0},500)
		});
		// 到底部按钮事件
		$("#backBottom").click(function(){
			var vh = $("#reply")[0].scrollHeight+$("#themeSection").height()-$(window).height()+44;			
			$("html,body").animate({scrollTop:vh},500);
		});
	/**********************请求***********************************/
	var dataCount = 0;
	var themeHtml = "";
	var replyHtml = "";
	var sectionId = "";
	$.ajax({
		type:"post",
		url:"/theme",
		async:true
	}).done(function(status,resData,jqXHR){
		themeHtml = template("themeSection",resData.theme);
		$("#themeSection").html(themeHtml);
		sectionId = resData.theme.sectionId;
		if(resData.reply){
			replyHtml = template("replyContainer",resData);
			$("#themeSection").html(replyHtml);
			dataCount = resData.reply.length;
			$(".dataCount").text(dataCount);
		}
		if(resData.theme.authorId == $.cookie("userId")){
			$(".settings").fadeIn();
			$(".deleteReply").show().parent().css("width",230);
		}
	}).fail(function(jqXHR,status,error){
		console.log(error);
	});	
	var favaCount = "";
	// 点赞数加1
   	$("#themeSection").on("click",".likeWC",function(){
   		var arg = arguments;
	 	favaCount = $(this).next().text();
	 	if(!$(this).data("like")){
	 		favaCount++;
   			$(this).removeClass("glyphicon-heart-empty").addClass("glyphicon-heart").next().text(favaCount);
   			$(this).data("like",true);
	 	}else{
	 		favaCount--;
   			$(this).addClass("glyphicon-heart-empty").removeClass("glyphicon-heart").next().text(favaCount);
   			$(this).data("like",false);
	 	}
   	});
	
	
	// 顶部的删除按钮事件
	$("header").on("click",".deleteComment",function(){
		if(confirm("是否删除所有")){
			$.ajax({
				type:"post",
				url:"/deleteComment",
				data:sectionId,
				async:true
			}).done(function(status,resData,jqXHR){
				if(resData.code == 1){
					alert("删除成功");
					$("#reply").empty();
				}
			}).fail(function(jqXHR,status,error){
				alert("删除失败");
			});
		}
		
	});
	// 点击 评论的事件
	$("#reply").on("click","li",function(){
		$(this).addClass("animated flipInX").find(".guard").fadeIn(1000);
	});
	// 点击 取消按钮事件
	$("#reply").on("click",".gCancel",function(event){
		event.stopImmediatePropagation();
		$(this).parents("li").removeClass("animated flipInX").find(".guard").fadeOut(1000);		
	});
	// 点击回复按钮事件
	$("#reply").on("click",".gReply",function(event){
		event.stopImmediatePropagation();
		if(!$.cookie("userId")){
			if(confirm("您尚未登陆是否为您跳转到登陆界面")){
				location.href = "register2.html";
				$(this).parents("li").removeClass("animated flipInX").find(".guard").fadeOut(1000);		
			}
		}else{
			var data = $(this).parents("li").find(".sectionID").text();
			var authorId = $(this).parents("li").find(".authorId").text()
			var obj = {data,userId:$.cookie("userId"),authorId};
			$.get("/replySection",obj,function(){
			});
		}
	});
	// 点击删除按钮事件
	$("#reply").on("click",".deleteReply",function(event){
		event.stopImmediatePropagation();
		var data = $(this).parents("li").find(".sectionID").text();
		var obj = {sectionId,authorId:data}
		var $this = $(this).parents("li");
		$.get("/deleteReply",obj,function(resData){
			if(resData.code == 1){
				$this.fadeOut(600);
			}
		});
	});
	// 上拉加载
	$(window).scroll(function(){
		clearTimeout(timer);
		$(".shortcutBtn").fadeIn();
		timer = setTimeout(function(){
			$(".shortcutBtn").fadeOut();				
		},1000);
		var vx = $(window).scrollTop();
		var vh = $(window).height();
		var total = vx+vh;
		if(total>=totalHeight-10){
			$(".loading").css({backgroundImage:"url(../images/loading_010.gif)",backgroundRepeat:"no-repeat",backgroundPosition: "50% 0"}).find("div").hide();	
					var data = {
						dataCount,sectionId
					}
					$.ajax({
						type:"get",
						url:"/newReply",
						async:true,
						data:data
					}).done(function(resData){
						if(resData){							
							htmlStr = template("mainTemplate",resData);
   							$("#reply").append(htmlStr);
   							totalHeight = $("#reply")[0].scrollHeight+44+$("#themeSection").height();
   							dataCount += dataCount+resData.length;
   							$(".dataCount").text(dataCount);
							$(".loading").css("background-image","").find("div").show().find(".iconM").show().next().text("上拉加载"); 						
						}else{							
							$(".loading").css("background-image","").find("div").show().html("<p>没有更多的数据</p>");					
						}
					}).fail(function(jqXHR,status,error){
							$(".loading").css("background-image","").find("div").show().html("<p><span class='glyphicon glyphicon-remove'></span>&nbsp;加载失败</p>");					
					})
		}
	});
});


window.onbeforeunload = function(){
	if($(".likeWC").data("like")){
		$.post("/section",favaCount,function(){});
	};
}






